package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.math.BigDecimal;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.come.entity.Baby;
import org.come.entity.Goodstable;
import org.come.mouslisten.BabyPartsMouslisten;
import org.come.mouslisten.ChangeBabyMouslisten;
import org.come.mouslisten.ChoseChildTypeMouslisten;
import org.come.until.GoodsListFromServerUntil;

import com.tool.btn.BabyBtn;
import com.tool.btn.FormsOnOffBtn;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

 

public class TestChildJpanel extends JPanel {

    private TestChildCardJpanel testChildCardJpanel;
    private BabyBtn labAttr,// 点击跳到孩子属性界面
            labTalent;// 点击跳到孩子天赋界面
    private JLabel labchildhat,// 放置小孩帽子
            labchildclothes,// 放置小孩衣服
            labchildarms,// 放置小孩武器
            labchildshoes;// 放置小孩鞋子
    private JLabel labchildage,// 小孩年龄
            labchildImg;// 放置小孩的图像
    private JLabel[] labListChild = new JLabel[6];
    // 孩子 表id
    private BigDecimal babyid;

    public TestChildJpanel() throws Exception {
        if("水墨UI".equals(MyIsif.getStyle())) {
            this.setPreferredSize(new Dimension(578, 369));
            this.setLayout(null);
            this.setBackground(UIUtils.Color_BACK);
            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 1);
            offBtn.setBounds(578 - 37, 10, 25, 25);
            this.add(offBtn);
            // 6个框用来放置小孩头像
            for (int i = 0; i < 6; i++) {
                labListChild[i] = new JLabel();
                ChangeBabyMouslisten mouslisten = new ChangeBabyMouslisten(i);
                labListChild[i].setBounds(57, 37 + i * 51, 39, 39);
                labListChild[i].setForeground(Color.WHITE);
                labListChild[i].setFont(UIUtils.TEXT_FONT);
                labListChild[i].addMouseListener(mouslisten);
                this.add(labListChild[i]);
            }
            // 小孩衣服
            labchildclothes = new JLabel("衣 服", SwingConstants.CENTER);
            labchildclothes.setBounds(358 + 26, 295 - 18, 52, 50);
            labchildclothes.setForeground(new Color(177, 162, 107));
            labchildclothes.addMouseListener(new BabyPartsMouslisten(0));
            labchildclothes.setFont(UIUtils.TEXT_FONT);
            this.add(labchildclothes);
            // 小孩帽子
            labchildhat = new JLabel("帽 子", SwingConstants.CENTER);
            labchildhat.setBounds(298 + 26, 295 - 18, 52, 50);
            labchildhat.setForeground(new Color(177, 162, 107));
            labchildhat.addMouseListener(new BabyPartsMouslisten(1));
            labchildhat.setFont(UIUtils.TEXT_FONT);
            this.add(labchildhat);
            // 小孩鞋子
            labchildshoes = new JLabel("鞋 子", SwingConstants.CENTER);
            labchildshoes.setBounds(478 + 26, 295 - 18, 52, 50);
            labchildshoes.setForeground(new Color(177, 162, 107));
            labchildshoes.addMouseListener(new BabyPartsMouslisten(2));
            labchildshoes.setFont(UIUtils.TEXT_FONT);
            this.add(labchildshoes);
            // 小孩武器
            labchildarms = new JLabel("武 器", SwingConstants.CENTER);
            labchildarms.setBounds(418 + 26, 295 - 18, 52, 50);
            labchildarms.setForeground(new Color(177, 162, 107));
            labchildarms.addMouseListener(new BabyPartsMouslisten(3));
            labchildarms.setFont(UIUtils.TEXT_FONT);
            this.add(labchildarms);
            // 小孩年龄
            labchildage = new JLabel("", SwingConstants.CENTER);
            labchildage.setForeground(Color.YELLOW);
            labchildage.setFont(new Font("宋体", Font.BOLD, 16));
            labchildage.setBounds(162, 54, 100, 20);
            this.add(labchildage);

            testChildCardJpanel = new TestChildCardJpanel();
            testChildCardJpanel.setBounds(293 + 26, 69 - 12, 240, 215);
            this.add(testChildCardJpanel);

            // 小孩属性点击按钮
            labAttr = new BabyBtn("skin/8100226/button/B180.png", 1);
            labAttr.setBounds(333, 48 - 17, 63, 26);
            labAttr.addMouseListener(new ChoseChildTypeMouslisten(1, testChildCardJpanel));
            this.add(labAttr);
            // 小孩天赋点击按钮
            labTalent = new BabyBtn("skin/8100226/button/B181.png", 1);
            labTalent.setBounds(333 + 65, 48 - 17, 63, 26);
            labTalent.addMouseListener(new ChoseChildTypeMouslisten(2, testChildCardJpanel));
            this.add(labTalent);
        }else{
            this.setPreferredSize(new Dimension(552, 378));
            this.setLayout(null);
            this.setBackground(UIUtils.Color_BACK);
            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/1/s74.png", 1, 1);
            offBtn.setBounds(532, 0, 25, 25);
            this.add(offBtn);
            // 6个框用来放置小孩头像
            for (int i = 0; i < 6; i++) {
                labListChild[i] = new JLabel();
                labListChild[i].setText(" 暂无");
                ChangeBabyMouslisten mouslisten = new ChangeBabyMouslisten(i);
                labListChild[i].setBounds(31, 49 + i * 51, 39, 39);
                labListChild[i].setForeground(Color.WHITE);
                labListChild[i].setFont(UIUtils.TEXT_FONT);
                labListChild[i].addMouseListener(mouslisten);
                this.add(labListChild[i]);
            }
            // 小孩衣服
            this.labchildclothes = new JLabel("衣 服", 0);
            this.labchildclothes.setBounds(357, 295, 52, 50);
            this.labchildclothes.setForeground(new Color(177, 162, 107));
            this.labchildclothes.addMouseListener(new BabyPartsMouslisten(0));
            this.labchildclothes.setFont(UIUtils.TEXT_FONT);

            this.add(this.labchildclothes);
            this.labchildhat = new JLabel("帽 子", 0);
            this.labchildhat.setBounds(297, 295, 52, 50);
            this.labchildhat.setForeground(new Color(177, 162, 107));
            this.labchildhat.addMouseListener(new BabyPartsMouslisten(1));
            this.labchildhat.setFont(UIUtils.TEXT_FONT);
            this.add(this.labchildhat);

            this.labchildshoes = new JLabel("鞋 子", 0);
            this.labchildshoes.setBounds(477, 295, 52, 50);
            this.labchildshoes.setForeground(new Color(177, 162, 107));
            this.labchildshoes.addMouseListener(new BabyPartsMouslisten(2));
            this.labchildshoes.setFont(UIUtils.TEXT_FONT);
            this.add(this.labchildshoes);

            this.labchildarms = new JLabel("武 器", 0);
            this.labchildarms.setBounds(417, 295, 52, 50);
            this.labchildarms.setForeground(new Color(177, 162, 107));
            this.labchildarms.addMouseListener(new BabyPartsMouslisten(3));
            this.labchildarms.setFont(UIUtils.TEXT_FONT);
            this.add(this.labchildarms);

            this.labchildage = new JLabel("", 0);
            this.labchildage.setForeground(Color.YELLOW);
            this.labchildage.setFont(new Font("宋体", 1, 16));
            this.labchildage.setBounds(142, 54, 100, 20);
            this.add(this.labchildage);

            this.testChildCardJpanel = new TestChildCardJpanel();
            this.testChildCardJpanel.setBounds(293, 69, 240, 214);
            this.add(this.testChildCardJpanel);

            // 小孩属性点击按钮
            labAttr = new BabyBtn("inkImg/hongmu/2/childattr_checked.png", 1);
            labAttr.setBounds(303, 48, 75, 26);
            labAttr.addMouseListener(new ChoseChildTypeMouslisten(1, testChildCardJpanel));
            this.add(labAttr);
            // 小孩天赋点击按钮
            labTalent = new BabyBtn("inkImg/hongmu/2/childtalent_unchecked.png", 1);
            labTalent.setBounds(367, 48, 75, 26);
            labTalent.addMouseListener(new ChoseChildTypeMouslisten(2, testChildCardJpanel));
            this.add(labTalent);
        }
    }

    ImageIcon icon;
    ImageIcon icon2;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if("水墨UI".equals(MyIsif.getStyle())) {
            // 小孩信息栏
            if (icon == null) {
                icon = new ImageIcon("inkImg/newbackground/B216.png");
            }
            g.drawImage(icon.getImage(), 0, 0, 578, 369, this);
            if (icon2 != null) {
                g.drawImage(icon2.getImage(), 112, 34, 199, 299, this);
            }
        }else{
            // 小孩信息栏
            if (icon == null) {
                icon = new ImageIcon("inkImg/hongmu/2/71_png.xy2uiimg.child_bg.png");
            }
            g.drawImage(icon.getImage(), 0, 0, 552, 378, this);
            if (icon2 != null) {
                g.drawImage(icon2.getImage(), 86, 47, 199, 299, this);
            }
        }
    }

    /**
     * 装备更改
     */
    public boolean ChangeParts(Baby baby, Goodstable good1, Goodstable good2, int type) {
        if (!GoodsListFromServerUntil.ChangeParts(good1, good2)) {
            return false;
        }
        if (good1 != null) {
            GoodsListFromServerUntil.fushis.remove(good1.getRgid());
        }
        if (good2 != null) {
            if (type == 1) {
                labchildhat.setIcon(GoodsListFromServerUntil.imgpathAdaptive(good2.getSkin(),49,49));
                labchildhat.setText(null);// 小孩帽子
            } else if (type == 0) {
                labchildclothes.setIcon(GoodsListFromServerUntil.imgpathAdaptive(good2.getSkin(),49,49));
                labchildclothes.setText(null);// 小孩衣服
            } else if (type == 3) {
                labchildarms.setIcon(GoodsListFromServerUntil.imgpathAdaptive(good2.getSkin(),49,49));
                labchildarms.setText(null);// 小孩武器
            } else if (type == 2) {
                labchildshoes.setIcon(GoodsListFromServerUntil.imgpathAdaptive(good2.getSkin(),49,49));
                labchildshoes.setText(null);// 小孩鞋子
            }
            GoodsListFromServerUntil.fushis.put(good2.getRgid(), good2);
        } else {
            if (type == 1) {
                labchildhat.setIcon(null);
                labchildhat.setText("帽 子");// 小孩帽子
            } else if (type == 0) {
                labchildclothes.setIcon(null);
                labchildclothes.setText("衣 服");// 小孩衣服
            } else if (type == 3) {
                labchildarms.setIcon(null);
                labchildarms.setText("武 器");// 小孩武器
            } else if (type == 2) {
                labchildshoes.setIcon(null);
                labchildshoes.setText("鞋 子");// 小孩鞋子
            }
        }
        testChildCardJpanel.getChildAttributeJpanel().showBaby(baby, baby.getpartAll());
        return true;
    }

    /**
     * 年龄展示
     */
    public void ShowAge(int age, int sex, String outcome) {
        int mouth = (age / 30) % 12;
        int year = age / 360;
        if (outcome == null || "".equals(outcome)) {
            StringBuffer buffer = new StringBuffer();
            if (year > 0) {
                buffer.append(year);
                buffer.append("岁");
            }
            buffer.append(mouth);
            buffer.append("月");
            labchildage.setText(buffer.toString());
        } else {
            labchildage.setText(outcome);
        }
        year = year / 3;
        if (year > 5) {
            year = 5;
        }
        if (sex == 0) {
            if (p != year) {
                p = year;
                icon2 = new ImageIcon("inkImg/old/baby/c110" + year + ".png");
            }

        } else {
            if (p != year + 10) {
                p = year + 10;
                icon2 = new ImageIcon("inkImg/old/baby/c100" + year + ".png");
            }
        }
    }

    int p = -1;

    /**
     * 孩子信息展示
     * 
     * @return
     */
    public void ShowBaby(Baby baby) {
        babyid = baby != null ? baby.getBabyid() : null;
        BigDecimal[] bigs = baby != null ? baby.getpartAll() : null;
        testChildCardJpanel.getChildAttributeJpanel().showBaby(baby, bigs);
        testChildCardJpanel.getChildTalentJpanel().showBaby(baby);
        if (baby == null) {
            // 清空界面
            icon2 = null;
            labchildage.setText("");
            labchildhat.setIcon(null);
            labchildhat.setText("帽 子");// 小孩帽子
            labchildclothes.setIcon(null);
            labchildclothes.setText("衣 服");// 小孩衣服
            labchildarms.setIcon(null);
            labchildarms.setText("武 器");// 小孩武器
            labchildshoes.setIcon(null);
            labchildshoes.setText("鞋 子");// 小孩鞋子
        } else {
            ShowAge(baby.getBabyage(), baby.getChildSex(), baby.getOutcome());
            Goodstable naozi = GoodsListFromServerUntil.fushis.get(bigs[1]);
            if (naozi == null) {
                labchildhat.setIcon(null);
                labchildhat.setText("帽 子");// 小孩帽子
            } else {
                labchildhat.setIcon(GoodsListFromServerUntil.imgpathAdaptive(naozi.getSkin(),49,49));
                labchildhat.setText(null);// 小孩帽子
            }
            Goodstable yifu = GoodsListFromServerUntil.fushis.get(bigs[0]);
            if (yifu == null) {
                labchildclothes.setIcon(null);
                labchildclothes.setText("衣 服");// 小孩衣服
            } else {
                labchildclothes.setIcon(GoodsListFromServerUntil.imgpathAdaptive(yifu.getSkin(),49,49));
                labchildclothes.setText(null);// 小孩衣服
            }
            Goodstable wuqi = GoodsListFromServerUntil.fushis.get(bigs[3]);
            if (wuqi == null) {
                labchildarms.setIcon(null);
                labchildarms.setText("武 器");// 小孩武器
            } else {
                labchildarms.setIcon(GoodsListFromServerUntil.imgpathAdaptive(wuqi.getSkin(),49,49));
                labchildarms.setText(null);// 小孩武器
            }
            Goodstable xuezi = GoodsListFromServerUntil.fushis.get(bigs[2]);
            if (xuezi == null) {
                labchildshoes.setIcon(null);
                labchildshoes.setText("鞋 子");// 小孩鞋子
            } else {
                labchildshoes.setIcon(GoodsListFromServerUntil.imgpathAdaptive(xuezi.getSkin(),49,49));
                labchildshoes.setText(null);// 小孩鞋子
            }
        }
    }

    public TestChildCardJpanel getTestChildCardJpanel() {
        return testChildCardJpanel;
    }

    public void setTestChildCardJpanel(TestChildCardJpanel testChildCardJpanel) {
        this.testChildCardJpanel = testChildCardJpanel;
    }

    public JLabel getLabchildhat() {
        return labchildhat;
    }

    public void setLabchildhat(JLabel labchildhat) {
        this.labchildhat = labchildhat;
    }

    public JLabel getLabchildclothes() {
        return labchildclothes;
    }

    public void setLabchildclothes(JLabel labchildclothes) {
        this.labchildclothes = labchildclothes;
    }

    public JLabel getLabchildarms() {
        return labchildarms;
    }

    public void setLabchildarms(JLabel labchildarms) {
        this.labchildarms = labchildarms;
    }

    public JLabel getLabchildshoes() {
        return labchildshoes;
    }

    public void setLabchildshoes(JLabel labchildshoes) {
        this.labchildshoes = labchildshoes;
    }

    public JLabel[] getLabListChild() {
        return labListChild;
    }

    public void setLabListChild(JLabel[] labListChild) {
        this.labListChild = labListChild;
    }

    public JLabel getLabchildage() {
        return labchildage;
    }

    public void setLabchildage(JLabel labchildage) {
        this.labchildage = labchildage;
    }

    public JLabel getLabchildImg() {
        return labchildImg;
    }

    public void setLabchildImg(JLabel labchildImg) {
        this.labchildImg = labchildImg;
    }

    public BigDecimal getBabyid() {
        return babyid;
    }

    public void setBabyid(BigDecimal babyid) {
        this.babyid = babyid;
    }

    public BabyBtn getLabAttr() {
        return labAttr;
    }

    public void setLabAttr(BabyBtn labAttr) {
        this.labAttr = labAttr;
    }

    public BabyBtn getLabTalent() {
        return labTalent;
    }

    public void setLabTalent(BabyBtn labTalent) {
        this.labTalent = labTalent;
    }

}
